//
//  CollectionViewController.swift
//  HiBook
//
//  Created by Apple on 16/8/13.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit
import WebKit

class CollectionViewController: CommonViewController,WKUIDelegate ,WKNavigationDelegate ,WKScriptMessageHandler{
    
    var ebookWebView : WKWebView?
    
    var hidden : Bool?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setWebView()
        loadWebView()
        self.view.addSubview(ebookWebView!)
        self.navigationController?.navigationBarHidden = true
        
        // Do any additional setup after loading the view.
    }

    func loadWebView(){
        
        let request = NSURLRequest(URL: NSURL(string: "http://\(MAIN_IP):8000/web/bookCollection.html")!)
        
        self.ebookWebView?.loadRequest(request)
        
    }
    
    func setWebView(){
        
        let config = WKWebViewConfiguration()
        
        self.ebookWebView = WKWebView(frame: CGRectMake(0, 10, SCREEN_WIDTH, SCREEN_HEIGHT) ,configuration: config)
        
        self.ebookWebView?.UIDelegate = self
        
        self.ebookWebView?.navigationDelegate = self
        
        self.ebookWebView?.configuration.userContentController.addScriptMessageHandler(self, name: "responesToPhone")
        
        self.ebookWebView?.allowsBackForwardNavigationGestures = true
        
        self.ebookWebView?.scrollView.scrollEnabled = false
        
        
    }
    func responesToPhone(){
        self.dismissViewControllerAnimated(true) { 
            
        }
    }
    
    func userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage) {
        
        NSLog("方法名:\(message.name) 传回:\(message.body)")
        
        self.hidden = message.body as? Bool
        
        self.responesToPhone()
        
        
        
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
